/*
 * @Description:
 * @Author: qingyang
 * @Date: 2023-02-27 14:59:27
 * @LastEditors: qingyang
 * @LastEditTime: 2023-03-02 16:20:27
 */
import { ref, reactive } from 'vue';
import { getFacilityList } from '@/apis/eci/inventory/inventory';
import {
  CPageTableProps,
  ColumnSettingTypeEnum,
  TableFieldsSettingType
} from '@/components/CPageTable';

export const useList = (inventoryNo: string, readonly: boolean) => {
  const totalCount = ref(0);

  // 表格配置
  const tableOptions: CPageTableProps['tableOptions'] = {
    loadData,
    pagination: false,
    selection: false,
    rowKey: 'inventoryFacilityNo',
    border: true,
    extraTableOption: {
      showOverflow: true,
      maxHeight: '100%',
      scrollY: { enabled: true },
      rowConfig: { height: 68 }
    }
  };

  function loadData() {
    totalCount.value = 0;
    return getFacilityList(inventoryNo).then(res => {
      totalCount.value = res.length;
      return {
        records: res,
        total: totalCount.value
      };
    });
  }

  // 列表
  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '排放源/目录',
      field: 'sourceNameIndex',
      slot: 'sourceNameIndexSlot',
      minWidth: 200,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '设备型号/设备位置',
      field: 'facilityModel',
      slot: 'facilityModelSlot',
      width: 180,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '能源/燃料',
      field: 'materialName',
      overflow: false,
      slot: 'materialNameSlot',
      width: 200,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '数据来源',
      field: 'dataOrigin',
      slot: 'dataOriginSlot',
      width: 160,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '负责部门',
      field: 'chargeDepartment',
      width: 160,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '仪器校正等级',
      field: 'correctionGrade',
      slot: 'correctionGradeSlot',
      width: 250,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '关联关系编号',
      field: 'relationNo',
      slot: 'relationNo',
      fixed: 'right',
      width: 194,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 120,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ];
  if (readonly) {
    tableFieldsSetting.splice(7, 1);
  }

  const options = reactive<CPageTableProps>({
    title: '监测计划',
    tableOptions,
    tableFieldsSetting,
    enableFieldsSetting: true,
    isDetailPage: true,
    showPageHeader: !readonly
  });

  return {
    // 配置
    options,
    totalCount
  };
};
